package com.gx.smart.building.ui.login.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableBoolean;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.ToastUtils;
import com.gx.smart.base.BaseViewModel;
import com.gx.smart.building.ui.common.util.AppUserInfoUtil;
import com.gx.smart.lib.event.LoadingMessageEvent;
import com.gx.smart.lib.http.api.AuthApiService;
import com.gx.smart.lib.http.api.RetrofitManager;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.response.Auth;
import com.gx.wisestone.uaa.grpc.lib.auth.VerifyCodeResp;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class LoginViewModel extends BaseViewModel {
    private final MutableLiveData<Boolean> loginSuccess = new MutableLiveData<>();
    private final MutableLiveData<Boolean> verifyCodeSuccess = new MutableLiveData<>();
    public final ObservableField<String> phoneNumber = new ObservableField<>("admin");
    public final ObservableField<String> phonePassword = new ObservableField<>("Admin1234");
    public final ObservableInt tabIndex = new ObservableInt(0);
    public final ObservableBoolean verifyCodeEnable = new ObservableBoolean(true);
    private final MutableLiveData<Boolean> loginEnable = new MutableLiveData(true);

    public LoginViewModel(@NonNull Application application) {
        super(application);
    }

    public LiveData<Boolean> getLoginEnable() {
        return this.loginEnable;
    }

    //登录类型 1 账号; 2 手机号; 3 短信验证码
    //登录UAA
//    public LiveData<Boolean> login(String phone, String password, int loginType) {
//        loginEnable.setValue(false);
//        EventBus.getDefault().post(new LoadingMessageEvent(true));
//        AuthApiService.getInstance().login(phone, password, loginType, new CallBack<LoginResp>() {
//
//            @Override
//            public void callBack(LoginResp result) {
//                EventBus.getDefault().post(new LoadingMessageEvent(false));
//                loginEnable.setValue(true);
//                if (result == null) {
//                    loginSuccess.setValue(false);
//                    ToastUtils.showLong("服务器超时");
//                    return;
//                }
//                if (result.getCode() == 100) {
//                    AppUserInfoUtil.saveLogin(phone, result.getToken());
//                    loginApp();
//                } else {
//                    loginSuccess.setValue(false);
//                    ToastUtils.showLong(result.getMsg());
//                }
//
//            }
//        });
//        return loginSuccess;
//    }
    public LiveData<Boolean> login(String phone, String password, int loginType) {
        loginEnable.setValue(false);
        EventBus.getDefault().post(new LoadingMessageEvent(true));


        Call<Auth> call = RetrofitManager.getInstance().getAuthService().login();
        call.enqueue(new Callback<Auth>() {
            @Override
            public void onResponse(Call<Auth> call, Response<Auth> response) {
                EventBus.getDefault().post(new LoadingMessageEvent(false));
                loginEnable.setValue(true);
                Auth result = response.body();
                if (result == null) {
                    loginSuccess.setValue(false);
                    ToastUtils.showLong("服务器超时");
                    return;
                }
                if (result.getComResp().getCode() == 100) {
                    AppUserInfoUtil.saveLogin(phone, result.getToken());
                    ToastUtils.showLong("登录成功");
                    loginSuccess.setValue(true);
                } else {
                    loginSuccess.setValue(false);
                    ToastUtils.showLong(result.getComResp().getMsg());
                }
            }

            @Override
            public void onFailure(Call<Auth> call, Throwable t) {
                EventBus.getDefault().post(new LoadingMessageEvent(false));
                Logger.e(t,"error network");
                loginSuccess.setValue(false);
                ToastUtils.showLong("服务器超时");
            }
        });
        return loginSuccess;
    }

    //登录系统获取用户相关信息
    public LiveData<Boolean> loginApp() {
//        SysUserApiService.getInstance().login(
//                new CallBack<SysUserLoginResp>() {
//
//                    @Override
//                    public void callBack(SysUserLoginResp result) {
//                        if (result == null) {
//                            loginSuccess.setValue(false);
//                            ToastUtils.showLong("服务器超时");
//                            return;
//                        }
//                        ComResp comResp = result.getComResp();
//                        if (comResp.getCode() == 100) {
//                            ToastUtils.showLong("登录成功");
//                            SysUserDto data = result.getSysUserDto();
//                            AppUserInfoUtil
//                                    .saveAppLogin(data.getId(), data.getUaaUserId(), data.getName());
//                            loginSuccess.setValue(true);
//                        } else {
//                            loginSuccess.setValue(false);
//                            handleServiceException(comResp);
//                        }
//                    }
//                });
        return loginSuccess;
    }


    /**
     * //获取登录验证码
     * int targetType = 1;
     * int purpose = 1;
     */
    public LiveData<Boolean> getVerifyCode(String phone) {
        //获取登录验证码
        int targetType = 1;
        int purpose = 1;
        AuthApiService.getInstance().verifyCode(phone, targetType, purpose,
                new CallBack<VerifyCodeResp>() {

                    @Override
                    public void callBack(VerifyCodeResp result) {
                        if (result == null) {
                            verifyCodeSuccess.setValue(false);
                            ToastUtils.showLong("服务器超时");
                            return;
                        }

                        if (result.getCode() == 100) {
                            ToastUtils.showLong("获取验证码成功");
                            verifyCodeSuccess.setValue(true);
                        } else {
                            verifyCodeSuccess.setValue(false);
                            ToastUtils.showLong(result.getMsg());
                        }
                    }
                });
        return verifyCodeSuccess;
    }
}
